package com.guagua.blog.controller;

import com.guagua.blog.dto.CommentDTO;
import com.guagua.blog.entity.Comment;
import com.guagua.blog.service.CommentService;
import com.guagua.blog.vo.CommentVO;
import com.guagua.common.response.Result;
import com.guagua.system.security.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "评论管理", description = "评论相关接口")
@RestController
@RequestMapping("/api/comments")
@RequiredArgsConstructor
public class CommentController {
    
    private final CommentService commentService;
    
    @Operation(summary = "添加评论")
    @PostMapping
    public Result<Comment> addComment(@Valid @RequestBody CommentDTO commentDTO) {
        Long userId = SecurityUtils.getCurrentUserId();
        Comment comment = commentService.addComment(commentDTO, userId);
        return Result.success(comment);
    }
    
    @Operation(summary = "获取文章评论列表")
    @GetMapping("/article/{articleId}")
    public Result<List<CommentVO>> getCommentsByArticleId(
            @Parameter(description = "文章ID") @PathVariable Long articleId) {
        List<CommentVO> comments = commentService.getCommentsByArticleId(articleId);
        return Result.success(comments);
    }
    
    @Operation(summary = "删除评论")
    @DeleteMapping("/{commentId}")
    public Result<Boolean> deleteComment(@Parameter(description = "评论ID") @PathVariable Long commentId) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean success = commentService.deleteComment(commentId, userId);
        return Result.success(success);
    }
} 